<template>
    <div class="liquid-glass">
        <div class="col left">
            <!-- 背景大字 -->
            <div class="bg-text" aria-hidden="true">
                <template v-for="(ch,idx) in poetry.bgText" :key="idx">{{ ch }}<br v-if="idx===1" /></template>
            </div>
            <!-- 对句，两列竖排 -->
            <div class="couplet-wrapper">
                <div class="couplet first" v-if="poetry.couplet && poetry.couplet[0]">
                    <template v-for="(ch,idx) in poetry.couplet[0]" :key="'c1-'+idx">{{ ch }}<br /></template>
                </div>
                <div class="couplet second" v-if="poetry.couplet && poetry.couplet[1]">
                    <template v-for="(ch,idx) in poetry.couplet[1]" :key="'c2-'+idx">{{ ch }}<br /></template>
                </div>
            </div>
        </div>
        <div class="col right">
            <div class="content-text">
                <template v-for="(ch,idx) in poetry.content" :key="'ct-'+idx">{{ ch }}<br v-if="ch==='，' || ch==='。'" /></template>
            </div>
            <div class="meta">—— {{ poetry.author }}《{{ poetry.title }}》</div>
        </div>

        <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
            <defs>
                <filter id="glass-distortion" x="0%" y="0%" width="100%" height="100%">
                    <feTurbulence type="fractalNoise" baseFrequency="0.008 0.008" numOctaves="2" seed="92" result="noise" />
                    <feGaussianBlur in="noise" stdDeviation="2" result="blurred" />
                    <feDisplacementMap in="SourceGraphic" in2="blurred" scale="77" xChannelSelector="R" yChannelSelector="G" />
                </filter>
            </defs>
        </svg>
        <button class="speak-btn" @click="togglePlay" :aria-label="isPlaying ? '暂停播放' : '播放音频'">
            <svg v-if="!isPlaying" viewBox="0 0 100 100" class="icon">
                <polygon points="35,25 35,75 75,50" fill="currentColor" />
            </svg>
            <svg v-else viewBox="0 0 100 100" class="icon">
                <rect x="30" y="25" width="12" height="50" rx="2" fill="currentColor" />
                <rect x="58" y="25" width="12" height="50" rx="2" fill="currentColor" />
            </svg>
        </button>
    </div>
</template>
<script setup>
import { ref, watch, onMounted, onBeforeUnmount } from 'vue'

// Props: poetry 对象（必需），audioBasePath 可覆盖音频基础路径（默认 assets/audio），autoPlay 可选
const props = defineProps({
    poetry: {
        type: Object,
        required: true
    },
    audioBasePath: {
        type: String,
        default: () => '/src/assets/audio/'
    },
    autoPlay: {
        type: Boolean,
        default: false
    }
})

// 直接使用 props.poetry 里的字段
const poetry = props.poetry
const isPlaying = ref(false)
let audioEl = null

function setupAudio(){
    cleanupAudio()
    if(!poetry || !poetry.audio) return
    // 利用动态导入的方式，若传入的是完整路径则直接用
    const src = poetry.audio.startsWith('http') || poetry.audio.startsWith('/')
        ? poetry.audio
        : new URL(`/src/assets/audio/${poetry.audio}`, import.meta.url).href
    audioEl = new Audio(src)
    audioEl.preload = 'auto'
    audioEl.addEventListener('ended', ()=>{ isPlaying.value = false })
    audioEl.addEventListener('pause', ()=>{ if(audioEl && audioEl.currentTime < audioEl.duration) { isPlaying.value = false } })
    if(props.autoPlay){
        audioEl.play().then(()=>{ isPlaying.value = true }).catch(()=>{})
    }
}

function togglePlay(){
    if(!audioEl) return
    if(!isPlaying.value){
        audioEl.play().then(()=>{ isPlaying.value = true }).catch(()=>{})
    } else {
        audioEl.pause()
        isPlaying.value = false
    }
}

function cleanupAudio(){
    if(audioEl){
        audioEl.pause()
        audioEl.src = ''
        audioEl = null
    }
}

onMounted(()=>{ setupAudio() })
watch(()=>props.poetry, ()=>{ setupAudio() }, { deep: true })
onBeforeUnmount(()=>{ cleanupAudio() })
</script>
<style lang="scss" scoped>
.liquid-glass {
    width: 100%;
    min-height: 100vh;
    border-radius: 28px;
    position: relative;
    box-shadow: 0px 6px 24px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: none;
    padding: 0;
    margin: 0;
    text-decoration: none;
}

.liquid-glass:focus {
    outline: none;
}

.liquid-glass::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 0;
    border-radius: 28px;
    box-shadow: inset 0 0 15px -5px #ffffff70;
    background-color: rgba(255, 255, 255, 0);
}

.liquid-glass::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: 56px;
    backdrop-filter: blur(0px);
    -webkit-backdrop-filter: blur(0px);
    filter: url(#glass-distortion);
    -webkit-filter: url(#glass-distortion);
}

/* --- 新布局样式 --- */
.col {
    position: relative;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* 4:6 比例 */
.left { flex: 0 0 40%; }
.right { flex: 0 0 60%; }

.left {
    padding: 16px 8px;
}

.right {
    padding: 16px 8px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 24px;
}

.bg-text {
    position: absolute;
    top: 40px;
    left: 40px;
    width: 100%;
    height: 100%;
    font-size: 220px;
    line-height: 200px;
    font-family: 'twbzk', sans-serif;
    color: rgba(255,255,255, 0.1);
    text-orientation: mixed;
    letter-spacing: 4px;
    user-select: none;
    pointer-events: none;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0;
    z-index: 0;
}

.couplet-wrapper {
    position: relative;
    display: flex;
    height: 100%;
    min-height: 440px;
    justify-content: center;
    align-items: center;
    gap: 40px; /* 两列间距 */
    z-index: 1;
}

.couplet {
    font-family: 'twbzk', sans-serif;
    font-size: 32px;
    background: rgba(46, 95, 46, 0.8);
    color: #fff;
    padding: 4px;
    border-radius: 4px;
    text-orientation: mixed;
    line-height: 1.2em;
    position: relative;
}

/* 交错 40px: 第一列(右)不偏移，第二列(左)向下 40px */
.couplet.first {
    order: 2; /* 放右侧 */
}
.couplet.second {
    order: 1; /* 放左侧 */
    transform: translateY(40px);
}

.content-text {
    font-family: 'twbzk', sans-serif;
    font-size: 64px;
    color: #2e5f2e;
    line-height: 1.2em;
    text-orientation: mixed;
}

.meta {
    font-family: 'twbzk', sans-serif;
    font-size: 32px;
    color: #2e5f2e;
    writing-mode: horizontal-tb;
    align-self: flex-start;
}

.speak-btn {
    position: absolute;
    bottom: 24px;
    right: 24px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: transparent;
    color: rgba($color: #fff, $alpha: 0.7);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.25);
    transition: background .25s, transform .25s;
    z-index: 10;
}
.speak-btn:hover { background: rgba(46,95,46,0.95); }
.speak-btn:active { transform: scale(.92); }
.speak-btn .icon { width: 38px; height: 38px; }

/* --- 移动端响应式（示例阈值：<= 640px） --- */
@media (max-width: 640px) {
    .liquid-glass {
            width: 100%;
            height: auto;
            min-height: 480px;
            flex-direction: column; /* 改为上下结构 */
            padding: 12px 8px 56px; /* 给底部按钮留空间 */
    }
    .col { height: auto; }
    .left, .right { padding: 8px 8px; }
    .left{
        width: 100%;
        min-height: 360px;
    }
    .bg-text { font-size: 120px; left: 0;}
    .couplet { font-size: 28px; padding: 4px 4px; }
    .couplet.second { transform: translateY(24px); }
    .content-text { font-size: 40px; line-height: 1.15em; }
    .meta { font-size: 20px; }
    .speak-btn { width: 52px; height: 52px; bottom: 8px; right: 8px; }
    .speak-btn .icon { width: 30px; height: 30px; }
}
</style>
